import axios from "axios";
import Element from 'element-ui'
import store from './store'
import router from './router'

axios.defaults.baseURL = "http://localhost:8081"


//请求拦截器
axios.interceptors.request.use(config => {
  return config;
})

//响应拦截器
axios.interceptors.response.use(response => {
    //1.axios发送请求后，后端返回的数据
    let res = response.data;

    //2.根据后端返回来的code进行判断是否执行axios请求的.then()
    if (res.code === 200) {
      //继续往下走，return后才会执行axios的.then()里面的东西
      return response;
    } else {
      Element.Message.error(response.data.msg, {duration: 3 * 1000});
      //阻止请求进入后续    ！！就不会进入axios请求后的.then()里面的东西了！
      return Promise.reject(response.data.msg)
    }

  }, error => {
    console.log(error);
    //401 未登录的话
    if (error.response.status === 401) {
      store.commit("REMOVE_INFO");
      router.push({name:'Login'});
    }

    Element.Message.error(error.response.data.msg);
    //阻止请求进入后续
    return Promise.reject(error);
  }
)










